package com.yyw.gulimall.cart.config;

import com.yyw.common.constant.common.UrlConstant;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.DefaultCookieSerializer;

/**
 * @Classname SpringSessionAndRedisConfig
 * @Description SpringSession / redis 配置类
 * 解决分布式环境下的Session共享问题
 * 解决同域下的Session共享问题(放大Session 作用域 将 auth.gulimall.com 的 session 放大至 gulimall.com)
 * @Date 2020/12/17 9:07
 * @Created yyw
 *
 * EnableRedisHttpSession 开启SpringSession功能，基于Redis的
 */
@Configuration
@EnableRedisHttpSession
public class SpringSessionAndRedisConfig {

    /**
     * 自定义 cookie 设置，可以设置cookie的各种信息
     * 此处只方法的cookie的域到父域了，这样子域之间就可以实现session共享
     * @return
     */
    @Bean
    public CookieSerializer cookieSerializer(){
        DefaultCookieSerializer cookieSerializer = new DefaultCookieSerializer();
        cookieSerializer.setDomainName(UrlConstant.COOKIE_DOMAIN_URL);
        return cookieSerializer;
    }

    @Bean
    public RedisSerializer<Object> springSessionDefaultRedisSerializer() {
        return new GenericJackson2JsonRedisSerializer();
    }
}
